package com.pointwest.pls.console;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import com.pointwest.pls.displayDomain.EmployeeSeat;
import com.pointwest.pls.util.Utilities;

public class Main {
	
	public static void main(String[] args) {

		BufferedReader reader = new BufferedReader(new InputStreamReader(
				System.in));
		try {
			showMainMenu(reader);
			reader.close();
		} catch (Exception exp) {

		} finally {
		}

	}

	private static void showMainMenu(BufferedReader reader) throws ClassNotFoundException, SQLException, Exception {
		Core core = new Core();
		boolean isReturnToMainMenu = true;
		boolean isValid;
		int employeeId = 0;
		int rowsChanged;
		String seatCode = null;
		String outputTextFile = null;

		do {
			initializeScreen();
			String response = getMainMenuResponse(reader);
			rowsChanged = 0;
			if (response != null) {

				switch (response) {
				case "1": // Search Employee
					System.out.println("Search Employee");
					employeeId = getIDResponse(reader);
					outputTextFile = core.searchEmployeeSeat(employeeId);
					exportSearchResultToTextFile(reader, outputTextFile, employeeId);
					break;
				case "2": // Update Employee Seat
					System.out.println("Update Employee Seat");
					rowsChanged = 0;
					employeeId = getIDResponse(reader);
					seatCode = getSeatCodeResponse(reader);
					isValid = core.checkSeatAvailability(seatCode);
					if(!isValid){
						rowsChanged = core.insertEmployeeSeat(seatCode, employeeId);
					} else{
						rowsChanged = core.updateEmployeeSeat(seatCode, employeeId);						
					}
					
					if (rowsChanged != 0){
						System.out.println("Seat Updated!");
					} else{
						System.out.println("Failed to update seat. Please check your seat number");
					}

					break;
				case "3": // Remove Seat Assignment
					System.out.println("Remove Seat Assignment");
					employeeId = getIDResponse(reader);
					seatCode = getSeatCodeResponse(reader);
					rowsChanged = 0;
					isValid = core.checkSeatAvailability(seatCode);
					if(!isValid){
						rowsChanged = core.removeSeatAssignment(seatCode, employeeId);
					} else{
						System.out.println("Employee not seated on " + seatCode);						
					}
					
					if (rowsChanged != 0){
						System.out.println("Employee removed from seat");
					} else{
						System.out.println("Failed to remove seat. Please check your seat number");
					}
					
					break;
				case "4":
					isReturnToMainMenu = false;
					System.out.println("Exiting application...");
					break;
				default :
					break;
				}
			}
		} while (isReturnToMainMenu);
		
		
	}

	/**
	 * Initializes the Main Menu screen
	 */
	private static void initializeScreen() {
		printAsterisks();
		printDashes();
		System.out.println("\tM\tA\tI\tN\t\tM\tE\tN\tU");
		printDashes();
		System.out.println("[1] Search Employee");
		System.out.println("[2] Update Employee Seat");
		System.out.println("[3] Remove Seat Assignment");
		System.out.println("[4] EXIT");
		printDashes();
		printAsterisks();
		System.out.println();
	}

	/**
	 * Gets the response of the user among the options in Main Menu
	 * @param reader
	 * @return user response
	 */
	private static String getMainMenuResponse(BufferedReader reader) {
		System.out.print("Choose from options above: ");
		String response = null;

		try {
			response = Utilities.getUserResponse(reader);
		} catch (Exception e) {
			// TODO: handle exception
		}

		return response;
	}
	
	/**
	 * Gets the ID of the employee to search for
	 * @param reader
	 * @return
	 */
	private static int getIDResponse(BufferedReader reader) {
		String idResponse = null;
		int employeeId = 0;
		boolean isValidResponse = false;
		
		do {
			try {
				System.out.print("Enter employee ID: ");
				idResponse = Utilities.getUserResponse(reader);
				employeeId = Integer.parseInt(idResponse);
				isValidResponse = true;
			} catch (NumberFormatException numFormExc) {
				System.out.println("INVALID INPUT: Employee Id must be in number format");
			} catch (Exception e) {
				// TODO: handle exception
			}
		} while (!isValidResponse);
		
		return employeeId;
	}
	
	/**
	 * Gets the seat code of a seat location
	 * @param reader
	 * @return
	 */
	private static String getSeatCodeResponse(BufferedReader reader) {
		String seatResponse = null;
		String seatCode = null;
		boolean isValidResponse = false;
		
		do {
			
			
			try {
				System.out.print("Enter Seat Code: ");
				seatResponse = Utilities.getUserResponse(reader);
				if (!seatResponse.isEmpty()){
					isValidResponse = true;
				} else{
					System.out.println("NULL INPUT: Please enter seat code");
				}

			} catch (Exception e) {
				// TODO: handle exception
			}
		} while (!isValidResponse);
		
		return seatResponse;
	}
	
	
	
	/**
	 * Exports the employee search result to text file
	 * @param reader
	 * @param employeeSearch
	 */
	private static void exportSearchResultToTextFile(BufferedReader reader, String outputTextFile, int employeeId)
	{
		Core core = new Core();
		String exportResponse = null;
		boolean isValidResponse = false;
		
		try {
			do {
				exportResponse = getExportTextFileResponse(reader);

				switch (exportResponse.toUpperCase()) {
				case "Y": // export result to text file
					core.exportEmployeeSeatInfoToTextFile(outputTextFile, employeeId);
					isValidResponse = true;
					break;
				case "N": // do not export
					System.out.println("No Export");
					isValidResponse = true;
					break;
				default:
					System.out.println("Reply with \"Y\" for YES, or \"N\" for NO.");
					break;
				}
			} while (!isValidResponse);

			
			
		} catch (Exception e) {
			// TODO: handle exception
		}

	}
	
	/**
	 * Gets the response of the user if a data is to be exported to a text file
	 * @param reader
	 * @return
	 */
	private static String getExportTextFileResponse(BufferedReader reader) {
		System.out.println("Export employee search result to text file? [Y/N]");
		String response = null;

		try {
				response = Utilities.getUserResponse(reader);

		} catch (Exception e) {
			// TODO: handle exception
		}
		
		return response;

	}


	/**
	 * Prints a sequence of dashes
	 */
	private static void printDashes() {
		System.out
				.println("----------------------------------------------------------------------------------");
	}

	/**
	 * Prints a sequence of asterisks
	 */
	private static void printAsterisks() {
		System.out
				.println("**********************************************************************************");
	}

}
